package com.onepice.mlp.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.onepice.mlp.core.service.SyslogService;
import com.onepice.mlp.data.entity.Syslog;
import com.onepice.mlp.utils.commom.ResponseResult;
import com.onepice.mlp.utils.enums.CodeType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author FireWang
 * @date 2022/07/11 23:25:03
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/syslog")
public class SyslogController {
    private SyslogService syslogService;
    private static JSONObject result;

    /**
     * 获取全部数据列表
     *
     * @return
     */
    @PostMapping("/list")
    public JSONObject getAll() {
        try {
            List<Syslog> syslogList = syslogService.getAll();
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("list", syslogList);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @GetMapping
    public JSONObject getById(@RequestParam Integer id) {
        try {
            Syslog syslog = syslogService.getById(id);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("data", syslog);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 新建
     *
     * @param syslog
     * @return
     */
    @PostMapping
    public JSONObject insert(@RequestBody Syslog syslog) {
        try {
            syslogService.insert(syslog);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新
     *
     * @param syslog
     * @return
     */
    @PutMapping
    public JSONObject update(@RequestBody Syslog syslog) {
        try {
            syslogService.update(syslog);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public JSONObject deleteById(@RequestParam Integer id) {
        try {
            syslogService.deleteById(id);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }
}
